1 package activity;
2
3 import java.lang.*;
4 import javax.swing.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import attr.*;
8
9 public class ManageCustomer extends JFrame implements ActionListener {
10 private JPanel panel;
11 ViewCustomerActivity prev;
12 private Customer customer;
13 private JButton buttonBack, buttonEdit, buttonDelete;
14 private JLabel title, header, userIdLabel, customerNameLabel, phoneNumberLabel, addressLabel;
15 private JTextField userIdTF, customerNameTF, phoneNumberTF, phoneCodeTF, addressTF;
16
17 public ManageCustomer(String cid, ViewCustomerActivity prev) {
18 super("Manage Customer");
19
20 this.setSize(500,400);
21 this.setResizable(false);
22 this.setLocationRelativeTo(null);
23 this.prev = prev;
24
25 customer = new Customer(cid);
26 customer.fetch();
27
28 panel = new JPanel();
29 panel.setLayout(null);
30 panel.setBackground(Theme.BACKGROUND_PANEL);
31
32 userIdLabel = new JLabel("Customer ID: "+customer.getUserId());
33 userIdLabel.setBounds(60, 20, 140, 30);
34 userIdLabel.setFont(Theme.FONT_INPUT);
35 panel.add(userIdLabel);
36
37 customerNameLabel = new JLabel("Name: ");
38 customerNameLabel.setBounds(60, 60, 140, 30);
39 customerNameLabel.setFont(Theme.FONT_INPUT);
40 panel.add(customerNameLabel);
41
42 phoneNumberLabel = new JLabel("Phone: ");
43 phoneNumberLabel.setBounds(60, 100, 140, 30);
44 phoneNumberLabel.setFont(Theme.FONT_INPUT);
45 panel.add(phoneNumberLabel);
46
47 addressLabel = new JLabel("Address: ");
48 addressLabel.setBounds(60, 140, 140, 30);
49 addressLabel.setFont(Theme.FONT_INPUT);
50 panel.add(addressLabel);
51
52 customerNameTF = new JTextField(customer.getCustomerName());
53 customerNameTF.setBounds(160, 60, 220, 30);
54 customerNameTF.setFont(Theme.FONT_INPUT);
55 panel.add(customerNameTF);
56
57 phoneCodeTF = new JTextField("+880");
58 phoneCodeTF.setEnabled(false);
59 phoneCodeTF.setBounds(160, 100, 40, 30);
60 phoneCodeTF.setFont(Theme.FONT_INPUT);
61 panel.add(phoneCodeTF);
62
63
64 phoneNumberTF = new JTextField(customer.getPhoneNumber().substring(4)+"");
65 phoneNumberTF.setBounds(200, 100, 180, 30);
66 phoneNumberTF.setFont(Theme.FONT_INPUT);
67 panel.add(phoneNumberTF);
68
69 addressTF = new JTextField(customer.getAddress()+"");
70 addressTF.setBounds(160, 140, 220, 30);
71 addressTF.setFont(Theme.FONT_INPUT);
72 panel.add(addressTF);
73
74 buttonEdit = new JButton("Edit");
75 buttonEdit.setBounds(60, 180, Theme.BUTTON_PRIMARY_WIDTH,30);
76 buttonEdit.setFont(Theme.FONT_BUTTON);
77 buttonEdit.setBackground(Theme.BACKGROUND_BUTTON_PRIMARY);
78 buttonEdit.setForeground(Theme.COLOR_BUTTON_PRIMARY);
79 buttonEdit.addActionListener(this);
80 panel.add(buttonEdit);
81
82 buttonDelete = new JButton("Delete");
83 buttonDelete.setBounds(180, 180, Theme.BUTTON_PRIMARY_WIDTH,30);
84 buttonDelete.setFont(Theme.FONT_BUTTON);
85 buttonDelete.setBackground(Theme.BACKGROUND_BUTTON_PRIMARY);
86 buttonDelete.setForeground(Theme.COLOR_BUTTON_PRIMARY);
87 buttonDelete.addActionListener(this);
88 panel.add(buttonDelete);
89
90 this.add(panel);
91 }
92
93 public void actionPerformed(ActionEvent ae) {
94 if (ae.getSource().equals(buttonEdit)) {
95 try {
96 customer.updateCustomer(customerNameTF.getText(),Integer.parseInt(phoneNumberTF.getText()),addressTF.getText().trim());
97 if (!prev.keywordTF.getText().trim().isEmpty())
98 prev.table.setModel(Customer.searchCustomer(prev.keywordTF.getText().trim(), prev.byWhatCB.getSelectedItem().toString()));
99 else
100 prev.table.setModel(Customer.searchCustomer("", "By Name"));
101 this.setVisible(false);
102 }
103 catch (NumberFormatException e) {
104 JOptionPane.showMessageDialog(this,"Invalid Input!");
105 }
106 }
107 else if (ae.getSource().equals(buttonDelete)) {
108 customer.deleteCustomer();
109 if (!prev.keywordTF.getText().trim().isEmpty())
110 prev.table.setModel(Customer.searchCustomer(prev.keywordTF.getText().trim(), prev.byWhatCB.getSelectedItem().toString()));
111 else
112 prev.table.setModel(Customer.searchCustomer("", "By Name"));
113 this.setVisible(false);
114 }
115 else {}
116 }
117 }